package com.nit.lab.mapper.basic;

import com.nit.lab.dto.basic.StudentDTO;
import com.nit.lab.entity.basic.StudentEntity;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @文件名：StudentMapper
 * @创建人: lxr
 * @创建时间: 2020/3/23
 * @描述:
 */
public interface StudentMapper {
    /**
     * @Description: 获取表格数据，查询条件自行编写对应代码，需区分模糊和相等，排序条件无需改动
     * @Author: lxr
     * @Date: 2020/03/23
     */
    @Select({"<script>SELECT " +
            "student_id AS `key`, student_id, " +
            "student_code, role_id, " +
            "student_name, gmt_create,phone " +
            "FROM basic_student " +
            "WHERE 1 = 1 " +
            "<if test='studentCode != null'> AND student_code LIKE CONCAT('%', #{studentCode}, '%') </if>" +
            "<if test='studentName != null'> AND student_name LIKE CONCAT('%', #{studentName}, '%') </if>" +
            "ORDER BY " +
            "<if test='sortField != null'>" +
            "${sortField} ${sortOrder}, " +
            "</if>" +
            "student_id desc " +
            "LIMIT #{start}, #{length}" +
            "</script>"})
    List<StudentDTO> list4Table(
            @Param("start") Integer start,
            @Param("length") Integer length,
            @Param("studentCode") String studentCode,
            @Param("studentName") String studentName,
            @Param("sortField") String sortField,
            @Param("sortOrder") String sortOrder) throws Exception;

    /**
     * @Description: 获取表格数据记录的总条数
     * @Author: lxr
     * @Date: 2020/03/23
     */
    @Select({"<script>SELECT COUNT(*) " +
            "FROM basic_student " +
            "WHERE 1 = 1 " +
            "<if test='studentCode != null'> AND student_code LIKE CONCAT('%', #{studentCode}, '%') </if>" +
            "<if test='studentName != null'> AND student_name LIKE CONCAT('%', #{studentName}, '%') </if>" +
            "</script>"})
    Long count4Table(
            @Param("studentCode") String studentCode,
            @Param("studentName") String studentName) throws Exception;

    /**
     * @Description: 新增记录
     * @Author: lxr
     * @Date: 2020/03/23
     */
    @Insert("INSERT INTO basic_student (" +
            "student_code, student_pwd, " +
            "student_name, role_id, gmt_create) " +
            "VALUES (#{studentEntity.studentCode}, #{studentEntity.studentPwd}, " +
            "#{studentEntity.studentName}, #{studentEntity.roleId}, " +
            "#{studentEntity.gmtCreate})")
    @Options(useGeneratedKeys = true, keyProperty = "studentId", keyColumn = "student_id")
    Integer add(@Param("studentEntity") StudentEntity studentEntity) throws Exception;

    /**
     * @Description: 登录验证
     * @Author: lxr
     * @Date: 2020/03/23
     */
    @Select({"SELECT * FROM basic_student WHERE student_code = #{studentCode} LIMIT 1"})
    StudentDTO getByLoginName(@Param("studentCode") String studentCode) throws Exception;

    /**
     * @Description: 根据学生Id获取信息
     * @Author: lxr
     * @Date: 2020/03/25
     */
    @Select({"SELECT * FROM basic_student WHERE student_id = #{studentId}"})
    StudentDTO getByStudentId(@Param("studentId") Long studentId) throws Exception;

    /**
     * @Description: 更新记录
     * @Author: lxr
     * @Date: 2020/03/23
     */
    @Update("UPDATE basic_student " +
            "SET student_code = #{studentEntity.studentCode}, " +
            "student_name = #{studentEntity.studentName}, " +
            "role_id = #{studentEntity.roleId} " +
            "WHERE student_id = #{studentEntity.studentId}")
    Integer update(@Param("studentEntity") StudentEntity studentEntity) throws Exception;

    /**
     * @Description: 删除记录
     * @Author: lxr
     * @Date: 2020/03/23
     */
    @Delete("DELETE FROM basic_student WHERE student_id = #{studentId}")
    Integer remove(@Param("studentId") Long studentId) throws Exception;

    /**
     * @Description: 重置密码
     * @Author: lxr
     * @Date: 2020/03/23
     */
    @Update({"UPDATE basic_student " +
            "SET student_pwd = #{studentPwd} " +
            "WHERE student_id = #{studentId}"
    })
    Integer resetPwd(@Param("studentId") Long studentId,
                     @Param("studentPwd") String studentPwd) throws Exception;
}
